package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyTravelQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;


    public PageInfo<?> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public Travel queryByTravelId(Long id) {
        return travelMapper.selectByTravelId(id);
    }

    public void save(Travel travel) {
        travel.setCreateTime(new Date());
        travel.setLastUpdateTime(new Date());
        //新增的游记,发布时间默认为null
        travelMapper.insert(travel);
        //获取id
        Long id  = travelMapper.selectId();
        if (travel.getTravelContent()!=null){
            //设置content主键
            travel.getTravelContent().setId(id);
            travelContentMapper.insert(travel.getTravelContent());
        }
    }

    public void update(Travel travel) {
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);
        //设置content主键
        travel.getTravelContent().setId(travel.getId());
        travelContentMapper.updateByPrimaryKey(travel.getTravelContent());
    }

    public TravelContent getContentById(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    public void changeState(Travel travel) {
        if (travel.getState()==2){
            travel.setReleaseTime(new Date());
        }
        travelMapper.changeState(travel);
    }

    public List<Travel> listTravelByStategyIdForApp(Long strategyId) {
        return travelMapper.listTravelByStategyIdForApp(strategyId);
    }

    public void deleteRelation(Long strategyId, Long travelId) {
        travelMapper.deleteRelation(strategyId,travelId);
    }
    public void insertRelation(Long strategyId, Long travelId) {
        travelMapper.insertRelation(strategyId,travelId);
    }
    public PageInfo listTravelCommentByStategyId(StrategyTravelQueryObject qo) {
        List<Map> list = travelMapper.listTravelCommentByStategyId(qo);
        return new PageInfo(list);
    }
    public List<Travel> selectRelease() {
        return travelMapper.selectRelease();
    }

    public int queryForCount(Long authorId) {
        return travelMapper.selectForCount(authorId);
    }

    @Override
    public Integer queryCollectCount(Long travelId) {
        Integer collectCount = travelMapper.selectCollectCount(travelId);
        return collectCount;
    }

    @Override
    public void updateCollectCount(int collectCount, Long travelId) {
        travelMapper.updateCollectCount(collectCount,travelId);
    }

    /**
     * 更新评论数
     * @param commentCount
     * @param travelId
     */
    @Override
    public void updateCommentCount(int commentCount, Long travelId) {
        travelMapper.updateCommentCount(commentCount,travelId);
    }

    @Override
    public PageInfo<?> queryForSearchList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForSearchList(qo);
        return new PageInfo<>(list);
    }


    /**
     * 通过游记id查询当前游记有多少点赞数
     * @param travelId
     * @return
     */
    @Override
    public Integer queryForLikeCount(Long travelId) {
       return travelMapper.selectForLikeCount(travelId);

    }

    /**
     * 更新点赞数量
     * @param likeCount
     * @param travelId
     */
    @Override
    public void updateLikeCount(Integer likeCount,Long travelId) {
        travelMapper.updateForLikeCount(likeCount,travelId);
    }
}
